﻿using B.R.RabbitRoot.Domain;
using B.R.RabbitRoot.ErrorCode;
using B.R.RabbitRoot.Infrastructure;
using B.R.RabbitRoot.Read.API.Applications.Commands.FarmInfoCommands;
using B.R.RabbitRoot.Read.API.Applications.Commands.FarmingCommands;
using B.R.RabbitRoot.Read.API.Applications.Commands.HealthCareInfoCommands;
using B.R.RabbitRoot.Read.API.Applications.Commands.LoginCommands;
using B.R.RabbitRoot.Read.API.Applications.Commands.SourceInfoCommands;
using B.R.RabbitRoot.Read.API.Dtos;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace B.R.RabbitRoot.Read.API.Controllers
{
    /// <summary>
    /// 控制器 API-读的
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ManagermentController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly RabbitDbcontext db;
        public ManagermentController(IMediator mediator,RabbitDbcontext db)
        {
            this.mediator = mediator;
            this.db = db;   
        }
        /// <summary>
        /// 溯源的显示
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<APIPaging<SourceInfoDTO>>> GetSourceInfo(int pageIndex,int pageSize,int sourceInfoId)
        {
            SourceInfoQueryCommand command = new SourceInfoQueryCommand();
            command.PageIndex = pageIndex;
            command.PageSize = pageSize;
            command.SourceInfoId = sourceInfoId;
            return await mediator.Send(command);
        }

        /// <summary>
        /// 获取商品
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<List<product>>> Getproduct()
        {
            productQueryCommand command=new productQueryCommand();
            return await mediator.Send(command);    
        }
        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<AccountDTO>> LoginAccount(LoginQueryCommand command)
        { 
           return await mediator.Send(command);
        }
        /// <summary>
        /// 养殖信息的显示
        /// </summary>
        /// <param name="sourceInfoId"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<FarmingDTO>> GetFarming(int sourceInfoId)
        {
            FarmingQueryCommand command= new FarmingQueryCommand();
            command.SourceInfoId = sourceInfoId;
            return mediator.Send(command);
        }
        /// <summary>
        /// redis 的测试
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> RedsiRebbit()
        {
            //先判断数据库有没数据
            var list = await RedisHelper.GetAsync<List<Classpractice>>("key:class");
            if (list != null && list.Count() > 0)
            { 
                return Ok(list);
            }
            var entity = await db.Classpractices.ToListAsync();
            await RedisHelper.SetAsync("key:class",entity,TimeSpan.FromMinutes(10));
            return Ok(entity);
        }
        /// <summary>
        /// 保健信息的查询命令
        /// </summary>
        /// <param name="farmingDetailsId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<List<HealthCareInfoDTO>>> GetHealthCare(int farmingDetailsId)
        {
            HealthCareInfoQueryCommand command = new HealthCareInfoQueryCommand();
            command.FarmingDetailsId = farmingDetailsId;
            return await mediator.Send(command);
        }
        /// <summary>
        /// 获取企业信息
        /// </summary>
        /// <param name="sourceInfoId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<FarmInfoDTO>> GetfarmInfo(int sourceInfoId)
        {
            FarmInfoQueryCommand command= new FarmInfoQueryCommand();
            command.SourceInfoId = sourceInfoId;
            return await mediator.Send(command);
        }
    }
}
